const app = getApp()
Page({
    data: {
        navShow:false,
        type:[
            {title:'精品手机',img:''},
            {title:'爆款电脑',img:''},
            {title:'全新平板',img:''},
            {title:'耳机设备',img:''},
            {title:'手表饰品',img:''},
        ],
        cate:[
            {title:'小米1',img:''},
            {title:'小米2',img:''},
            {title:'小米3',img:''},
            {title:'小米4',img:''},
            {title:'小米5',img:''},
            {title:'小米6',img:''},
            {title:'小米7',img:''},
            {title:'小米8',img:''},
            {title:'小米9',img:''},
            {title:'小米10',img:''},
        ],
        typeIndex:0, // 分类索引
        cateIndex:0, // 品牌索引
        indicatorIndex:0, //指示点
        scrollTop:'',  // 顶部距离

        search:{
            filterWord: '',
            rows: 10,
            page: 1,
            tid:null,
        },
        pruduct:[],
        totalResult:'',  // 数量


        show:false,  // 显示加载

        TestType: "",
        
    },
    onLoad() {
        let minType = app.minType;
        app._init_(this)
        this.getType(minType)
        // my.setNavigationBar({
        //   backgroundColor: '#1677FF'
        // });
        
    },

    onShow(){
      let index = app.goPageNum;
      let sta = app.categorySta;
      if(sta){
        setTimeout(()=>{
          this.typeTap(0,index);
        },50)
      }
    },
    onHide(){
      app.categorySta = false;
    },


    getType(minType){
        app.other.type()
        .then(res=>{
            let type = res.data
            type = type.reverse();

            type.forEach(item=>{
              item.childrenList = item.childrenList.reverse();
            })

            this.setData({type})
            
            let sta = app.categorySta;
            let num = app.goPageNum;
            let index = 0;
            
            if(sta){
              index = num;
            }
            
            let cate = type[index].childrenList;
            this.setData({cate});

            if(minType){
              this.data.search.tid = cate[minType].id;
            }else{
              this.data.search.tid = cate[0].id;
            }
            
            setTimeout(()=>{
              if(minType){
                this.setData({
                  cateIndex: minType,
                })
              }else{
                this.setData({
                  cateIndex: 0,
                })
              }
            },80)
            

            this.submit();
        })
    },
    
    // 分类点击
    typeTap(e,sta){
        
        let search = this.data.search;
        search.filterWord = '';
        search.rows = 10;
        search.page = 1;
        search.tid = null;
        let index = 0;
        if(sta != undefined){
          index = sta
        }else{
          index = e.target.dataset.index
        }

        let cate = this.data.type[index].childrenList;

        this.setData({
            search:search,
            typeIndex:index,
            cateIndex: 0,
            cate:cate,
            pruduct:[] 
        })
        
        app.goPageNum = index;
        this.setSearch()
    },
    // 分类点击
     handleChange(index) {
        let search = this.data.search;
        search.filterWord = '';
        search.rows = 10;
        search.page = 1;
        search.tid = null;
        this.setData({
            search:search,
            cateIndex: index,
            pruduct:[] 
        });
        this.setSearch()
    },
    onChange(index) {
        this.setData({
            cateIndex: index,
        });
    },

    // 设置搜索参数
    setSearch(){
        const search = this.data.search,
        typeIndex = this.data.typeIndex,
        cateIndex = this.data.cateIndex;
        search.rows = 10;
        search.page = 1;
        search.tid = this.data.type[typeIndex].childrenList[cateIndex].id;

        console.log(88888, search.tid);
        if(search.tid == 2039){
          this.setData({
            TestType : "testGoods"
          })
        }else{
          this.setData({
            TestType : ""
          })
        }

        this.setData({
            search:search
        })
        this.submit()
    },
    

    // 跳转
    navigateTo(e){
        app.navigateTo(e)
    },
    navigateToBtn(e){
      let con = e.target.dataset
      let url = con.url + '?param=' + con.param + '&&goFun=' + con.goFun + '&&comePath=' + this.data.TestType
      my.navigateTo({
        url: url,
      })
    },

    definedGoPage(e){
      let item = e.target.dataset.item;
      my.navigateTo({
        url: '/pages/product/detail/detail?param=' + item.id + '&comePath=' + this.data.TestType
      })
    },



    // 输入完成
    onConfirm(e,refreshSta){
        let search = this.data.search;
        search.filterWord = e.detail.value;
        search.rows = 10;
        search.page = 1;
        if(!refreshSta){
          search.tid = null;
        }
        this.setData({
            search,
            pruduct:[] 
        })
        this.submit()
    },
   


    // 搜索
    submit(){
        app.product.page(this.data.search)
        .then(res=>{
            let totalResult = res.data.totalResult
            this.dataPromise(res.data.list)
            .then(res=>{
                let pruduct = [...this.data.pruduct,...res];
				console.log(pruduct)
                pruduct.forEach(item=>{
					console.log(item)
                  let aa = item.price + '';
                  item.priceFront = aa.split('.')[0];
                  item.priceLater = aa.split('.')[1];
                })
                
                this.setData({pruduct,totalResult})
            })
        })
    },
     // 转价格
    dataPromise(list){
        return new Promise((relose, reject)=>{
            list.forEach((item) => {
                item.category = JSON.parse(item.category)
                item.colors = JSON.parse(item.colors)
                item.match = JSON.parse(item.match)
                item.params = JSON.parse(item.params)
                item.slideshows = JSON.parse(item.slideshows)
                this.getminPrice(item.params)
                    .then(res => {
                        item.price = res;
                    })
                
            })
            relose(list)
        })
    },

    getminPrice(params){
        return new Promise((resole,reject)=>{
            let prices=[]
            for (let i = 0;i<params.length;i++){
                for (let t = 0; t < params[i].term_list.length; t++) {
                    for (let s = 0; s < params[i].term_list[t].specifications.length; s++) {
                        let price = (params[i].term_list[t].specifications[s].money / params[i].term_list[t].value / 100).toFixed(2)
						if (params[i].term_list[t].value == 14 || params[i].term_list[t].value == 24) {
							 price = (params[i].term_list[t].specifications[s].money / (params[i].term_list[t].value  * 7 )/ 100).toFixed(2)
						} 
                        prices.push(price)
                    }
                }
            }
            let minprice = Math.min.apply(null, prices);
            resole(minprice)
        })
    },  


    

    // 下拉刷新
    onPullDownRefresh() {
        let e={detail:{value:''}}
        this.onConfirm(e,true)
        setTimeout(() => {
            my.stopPullDownRefresh({
                complete(res) {
                }
            })
        }, 1000)
    },
    stopPullDownRefresh() {
        my.stopPullDownRefresh({
            complete(res) {
                // console.log(res, new Date())
            }
        })
    },
    //   下拉加载
    onReachBottom() {
        let search = this.data.search,
        pruduct = this.data.pruduct,
        totalResult = this.data.totalResult;
        try {
            // 判断是否还有数据需要加载
            if (pruduct.length < totalResult) {
                search.page++
                this.setData({search})
                this.submit();
            }
        } catch (e) {}
    },
});
